package com.example.myapplication.logisticsinquiry;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.ViewapteTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LogisticsclassificationBean;
import com.example.myapplication.ShuJu.LogisticsinquircarouselmapBean;
import com.example.myapplication.ShuJu.LogisticsinquirynoBean;
import com.example.myapplication.apter.ExpresslistApter;
import com.example.myapplication.apter.NewsAdapter;
import com.example.myapplication.apter.ServerApter;
import com.google.gson.Gson;

import java.util.ArrayList;

import okhttp3.internal.tls.OkHostnameVerifier;

public class LogisticsinquiryActivity extends AppCompatActivity {

    private ImageView img;
    private RecyclerView re_hot;
    private RecyclerView re_list;
    private ViewPager vp;
    private SearchView search;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logisticsinquiry);
        initview();
        //返回
        but_bloak();
        //轮播图
        carouselmap();
        //分类
        logisticsclassification();
        //查询运单号
        querynumber();

    }

    private void querynumber() {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                String no = "ST0000001";
                if(no.equals(query)){
                    getlogisticsinquiryno();
                }else{
                    Toast.makeText(LogisticsinquiryActivity.this,"暂无该物流",Toast.LENGTH_SHORT).show();
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {


                return false;
            }
        });
    }

    private void getlogisticsinquiryno() {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/logistics_info/ST0000001",
                OKHttpTool.GET,
                LogisticsinquirynoBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LogisticsinquirynoBean logisticsinquirynoBean = (LogisticsinquirynoBean)obj;
                                if(logisticsinquirynoBean.getCode()==200){
                                    Intent intent = new Intent(LogisticsinquiryActivity.this,LogisticsinquirynoActivity.class);
                                    String logisticsinquirynoBeanString = gson.toJson(logisticsinquirynoBean);
                                    intent.putExtra(Commom.logisticsinquirynoBeanString,logisticsinquirynoBeanString);
                                    LogisticsinquiryActivity.this.startActivity(intent);
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void logisticsclassification() {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/logistics_company/list",
                OKHttpTool.GET,
                LogisticsclassificationBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LogisticsclassificationBean logisticsclassificationBean = (LogisticsclassificationBean)obj;
                                if(logisticsclassificationBean.getCode()==200){
                                    re_hot.setLayoutManager(new GridLayoutManager(LogisticsinquiryActivity.this,4));
                                    re_hot.setAdapter(new ServerApter(LogisticsinquiryActivity.this,logisticsclassificationBean.getData(),"LogisticsinquiryActivity"));
                                    //快递列表
                                    expresslist(logisticsclassificationBean);
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    //快递列表
    private void expresslist(LogisticsclassificationBean logisticsclassificationBean) {
        re_list.setLayoutManager(new LinearLayoutManager(this));
        re_list.setAdapter(new ExpresslistApter(this,logisticsclassificationBean.getData()));
    }

    private void carouselmap() {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/ad-banner/list",
                OKHttpTool.GET,
                LogisticsinquircarouselmapBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ArrayList<String> list = new ArrayList<>();
                                LogisticsinquircarouselmapBean logisticsinquircarouselmapBean = (LogisticsinquircarouselmapBean) obj;
                                if (logisticsinquircarouselmapBean.getCode() == 200){
                                    for (LogisticsinquircarouselmapBean.DataBean date : logisticsinquircarouselmapBean.getData()) {
                                        list.add(date.getImgUrl());
                                    }
                                    ViewapteTool viewapteTool = new ViewapteTool(LogisticsinquiryActivity.this, list);
                                    vp.setAdapter(viewapteTool);
                                    mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void but_bloak() {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogisticsinquiryActivity.this.finish();
            }
        });
    }

    private void initview() {
        img = findViewById(R.id.img);
        vp = findViewById(R.id.vp);
        re_hot = findViewById(R.id.re_hot);
        re_list = findViewById(R.id.re_list);
        search = findViewById(R.id.search);
        gson = new Gson();

    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                int index = vp.getCurrentItem();
                index = (index + 1) % 4;
                vp.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
            }
        }
    };

}